package com.example.a14110100420.yuejian.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.a14110100420.yuejian.R;
import com.example.a14110100420.yuejian.WActivity.picture.FabuPictureActivity;
import com.example.a14110100420.yuejian.WAdapter.picture.PictureListAdapter;
import com.example.a14110100420.yuejian.WSerivice.PictureService;
import com.example.a14110100420.yuejian.Wbase.Conast;
import com.example.a14110100420.yuejian.Wbean.picture.PictureList;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShaituFragment extends Fragment {
    private PictureListAdapter pictureListAdapter;
    private View view;
    private ImageView fabu;
    private RecyclerView recyclerView;

public ShaituFragment(){

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_shaitu, container, false);
        fabu = view.findViewById(R.id.fabu);

        recyclerView = view.findViewById(R.id.rev);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        onclick();
        fatieList();
        return view;

    }
    private void fatieList(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Conast.base_Url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        PictureService service = retrofit.create(PictureService.class);
        Call<List<PictureList>> call = service.pictureList();
        call.enqueue(new Callback<List<PictureList>>() {
            @Override
            public void onResponse(Call<List<PictureList>> call, Response<List<PictureList>> response) {
                List<PictureList> pictureLists = response.body();
                if (response.isSuccessful()){
                    pictureListAdapter = new PictureListAdapter(getActivity(),pictureLists);
                    recyclerView.setAdapter(pictureListAdapter);



                }
            }

            @Override
            public void onFailure(Call<List<PictureList>> call, Throwable t) {

            }
        });


    }
    public void onclick(){
        fabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),FabuPictureActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        fatieList();
    }
}
